<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <uni-search-bar 
        @input="input" 
        cancelButton="none"
        :radius="100"
      ></uni-search-bar>
    </view>
    <!-- 搜索列表 -->
    <view class="sugg-list" v-if="searchResults.length !== 0">
      <view 
        class="sugg-item" 
        v-for="(item, index) in searchResults" 
        :key="index"
        @click="gotoDetail(item)"
      >
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 历史记录 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17"  @click="clean"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag 
          :text="tag" 
          v-for="(tag, index) in historyList" 
          :key="index"
          @click="gotoGoodsList(tag)"
        ></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        keyword: '',
        timer: null,
        searchResults: [],
        historyList: []
      };
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('keyword') || '[]')
    },
    methods: {
      input(value) {
        // 防抖处理，清除延时器
        clearTimeout(this.timer)
        // 重启延时器，用一个变量接受timerId
        this.timer = setTimeout(() => {
          this.keyword = value
          this.getSearchList()
        }, 1000)
      },
      clean() {
        this.historyList = []
        uni.setStorageSync('keyword', '[]')
      },
      gotoGoodsList(tag) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + tag
        })
      },
      async getSearchList() {
        if (this.keyword.length === 0) return this.searchResults = []
        
        const { data } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.keyword })
        if (data.meta.status !== 200) return uni.$showMsg()
        this.searchResults = data.message
        
        this.saveSearchHistory()
      },
      saveSearchHistory() {
        // 往数组前面添加历史记录
        this.historyList.unshift(this.keyword)
        // 去除重复记录
        const set = new Set(this.historyList)
        this.historyList = Array.from(set)
        // 持久化存储
        uni.setStorageSync('keyword', JSON.stringify(this.historyList))
      },
      gotoDetail(item) {
        uni.navigateTo({
            // 指定详情页面的 URL 地址，并传递 goods_id 参数
            url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
          })
      }
    }
  }
</script>

<style lang="scss">
.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
.sugg-list {
  padding: 0 5px;
  .sugg-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 13px 0;
    font-size: 12px;
    border-bottom: 1px solid #efefef;
    .goods-name {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-right: 3px;
    }
  }
}
.history-box {
  padding: 0 5px;
  .history-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }
  .history-list {
    display: flex;
    flex-wrap: wrap;
    .uni-tag {
      margin-top: 5px;
      margin-right: 5px;
    }
  }
}
</style>
